<?php


namespace BoojasrFramework\DependencyInjection\Compiler;


use BoojasrFramework\ServicesContainer\ContainerBuilder;
use BoojasrFramework\ServicesContainer\DependencyInjection\Compiler\CompilerPassInterface;
use Twig\Extension\ExtensionInterface;

class TwigPass implements CompilerPassInterface
{

    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition("twig")){
            return ;
        }

        $twig = $container->getDefinition("twig");
        $taggedServices = $container->findTaggedServiceIds('twig.extension',true);
        foreach ($taggedServices as $id => $extensionDescription){
            $extension = $container->get($id);
            if ($extension instanceof ExtensionInterface){
                $twig->addMethodCall("addExtension",[$extension]);
            }
        }
    }
}
